package com.example.comprehension;

import android.annotation.SuppressLint;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.common.JSONCallback;
import com.example.comprehension.bean.LoginBean;
import com.example.comprehension.utils.HttpUtils;
import com.example.comprehension.utils.ObjectCacheToFile;
import com.example.comprehension.utils.pay.MyPayUtil;
import com.example.comprehension.utils.pay.PayListener;
import com.example.comprehension.view.AutoImageViewPager;
import com.example.comprehension.view.PagerMarkerView;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;

@SuppressLint("HandlerLeak")
public class BuyActivity extends BaseActivity {
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
		}
	};
	private Timer time = new Timer();
	private TimerTask task = new TimerTask() {
		@Override
		public void run() {
			mHandler.sendMessage(new Message());
		}
	};
	private AutoImageViewPager mViewPager;
	private GridView examGridView;
	private ExamGridAdapter adapter;
	private ViewHoldler holdler;
	private List<ImageView> mImageViewList = new ArrayList<ImageView>();
	private int[] imageIdsArr = { R.drawable.buypic1, R.drawable.buypic2,
			R.drawable.buypic3};
	private String buyUrl = "http://app2.gaofy.com:8080/gaofy/uploadBuyInfo";

	private TextView mTextView_Title;
	private LinearLayout mLinearLayout_Buy;
	private PagerMarkerView pmv;
	public static String Point_Key = "pointKey";
	private TextView tv_yuanjia,tv_youhui;
	private int buyType = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_buy);
		pmv = (PagerMarkerView) findViewById(R.id.pmv);
		tv_yuanjia = (TextView) findViewById(R.id.tv_yuanjia);
		tv_youhui = (TextView) findViewById(R.id.tv_youhui);
		init();
	}

	@Override
	public void initData() {
		super.initData();
		initImageView();
		examGridView.setAdapter(adapter);
		examGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				String name = BaseApplication.allTaoBeanPath.get(position);
				BaseApplication.examName = name;
				name += ".json";
				BaseApplication.examData = new HashMap<String, Object>();
				BaseApplication.judgeDifficulty(name);
				BaseApplication.taoBeans = BaseApplication.allTaoBeans
						.get(name);
				startActivity(ReadyActivity.createIntent(BuyActivity.this,
						BaseApplication.taoBeans
								.get(BaseApplication.currentExamLesson), name));
			}
		});

		OkHttpUtils.get()
				.url("http://app.gaofy.com/upload/config/androidPriReading/buy.json")
				.build().execute(new JSONCallback() {
			@Override
			public void onError(Call call, Exception e, int id) {

			}

			@Override
			public void onResponse(JSONObject response, int id) {
				buyType = response.optInt("buy");

				if (buyType == 1) {
					tv_yuanjia.setText("限时价 ￥6");
					tv_youhui.setVisibility(View.VISIBLE);
				}
			}
		});
	}

	@Override
	public void initWidget() {
		super.initWidget();
		mViewPager = (AutoImageViewPager) findViewById(R.id.view_pager);
		examGridView = (GridView) findViewById(R.id.examGridView);
		adapter = new ExamGridAdapter();
		mTextView_Title = (TextView) findViewById(R.id.text_title);
		mLinearLayout_Buy = (LinearLayout) findViewById(R.id.buy_layout);
		
		int point = getIntent().getIntExtra(Point_Key, 0);
		if(point==1)
		{
			mTextView_Title.setText("课程");
			mLinearLayout_Buy.setVisibility(View.GONE);
		}

		mViewPager.setImages(imageIdsArr);
	}

	public void onBuyClick(View v) {
		MyPayUtil pay = new MyPayUtil(BuyActivity.this, new PayListener() {

			@Override
			public void onPaying() {
				Toast.makeText(BuyActivity.this, "支付结果确认中", Toast.LENGTH_SHORT)
						.show();
			}

			@Override
			public void onPaySuccess() {
				Toast.makeText(BuyActivity.this, "支付成功", Toast.LENGTH_SHORT)
						.show();

				if (null != ObjectCacheToFile.getCache("LoginBean",
						LoginBean.class, BuyActivity.this)) {
					LoginBean olderLogin = (LoginBean) ObjectCacheToFile
							.getCache("LoginBean", LoginBean.class,
									BuyActivity.this);
					if ((null != olderLogin) && (null != olderLogin.data)) {
						buyUrl += "?userName=" + olderLogin.data.userName
								+ "&appName=" + Config.appName + "&point1=1&buyWhat=point1";
						new Thread(new Runnable() {
							@Override
							public void run() {
								HttpUtils.getPostResult(buyUrl);
							}
						}).start();
					}
				}
			}

			@Override
			public void onPayErr() {
				Toast.makeText(BuyActivity.this, "支付失败", Toast.LENGTH_SHORT)
						.show();

			}
		}, "购买题目", "30元购买题目", buyType == 0 ? "30.00" : "6.00");
		pay.start();
	}

	@Override
	public void onViewClick(View v) {
		super.onViewClick(v);
		switch (v.getId()) {
		case R.id.ib_back:
			time.cancel();
			task.cancel();
			finish();
			break;

		default:
			break;
		}
	}

	private void initImageView() {
		for (int i = 0; i < imageIdsArr.length; i++) {
			ImageView imageView = new ImageView(this);
			imageView.setBackgroundResource(imageIdsArr[i]);
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			mImageViewList.add(imageView);
		}

		pmv.setCount(imageIdsArr.length);
		pmv.setNoFocusBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.icon_no_foucs));
		pmv.setFocusBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.icon_foucs));
		mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

			}

			@Override
			public void onPageSelected(int position) {
				pmv.setCurrent(position % mImageViewList.size());
			}

			@Override
			public void onPageScrollStateChanged(int state) {

			}
		});
	}

	private class ExamGridAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return BaseApplication.allTaoBeans.size();
		}

		@Override
		public Object getItem(int arg0) {
			return BaseApplication.allTaoBeans.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			if (convertView == null) {
				holdler = new ViewHoldler();
				convertView = LayoutInflater.from(BuyActivity.this).inflate(
						R.layout.read_grid_item, null);
//				holdler.btn_item = (Button) convertView
//						.findViewById(R.id.btn_item);
				holdler.tv_title_1 = (TextView) convertView
						.findViewById(R.id.tv_title_1);
				holdler.tv_title_2 = (TextView) convertView
						.findViewById(R.id.tv_title_2);
				convertView.setTag(holdler);
			} else {
				holdler = (ViewHoldler) convertView.getTag();
			}
			String name = BaseApplication.allTaoBeanPath.get(position);
			String[] titles = name.split("_");
			holdler.tv_title_1.setText(titles[1].equals("1") ? "体验课程"
					: titles[1].equals("2") ? "中级题"
					: titles[1].equals("3") ? "高级题" : "体验课程");
			holdler.tv_title_2.setText("练习" + (Integer.valueOf(titles[2]) + 1));
			//holdler.btn_item.setTag(name);
//			holdler.btn_item.setOnClickListener(new OnClickListener() {
//
//				@Override
//				public void onClick(View v) {
//					String name = (String) v.getTag();
//					BaseApplication.examName = name;
//					name += ".json";
//					BaseApplication.examData = new HashMap<String, Object>();
//					BaseApplication.judgeDifficulty(name);
//					BaseApplication.initLesson();
//					BaseApplication.taoBeans = BaseApplication.allTaoBeans
//							.get(name);
//					startActivity(ReadyActivity.createIntent(BuyActivity.this,
//							BaseApplication.taoBeans
//									.get(BaseApplication.currentExamLesson),
//							name));
//				}
//			});
			return convertView;
		}

	}

	private class ViewHoldler {
		public Button btn_item;
		public TextView tv_title_1, tv_title_2;
	}
}
